package com.dzt.androidframework.view;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.dzt.androidframework.R;
import com.dzt.androidframework.activity.ActivityTitle;
import com.dzt.androidframework.utils.JToastKit;
import com.lljjcoder.Interface.OnCityItemClickListener;
import com.lljjcoder.bean.CityBean;
import com.lljjcoder.bean.DistrictBean;
import com.lljjcoder.bean.ProvinceBean;
import com.lljjcoder.citywheel.CityConfig;
import com.lljjcoder.style.citypickerview.CityPickerView;

import org.xutils.view.annotation.ViewInject;

public class CityPickerWheelActivity extends ActivityTitle {
	@ViewInject(R.id.pro_et)
	EditText mProEt;
	@ViewInject(R.id.city_et)
	EditText mCityEt;
	@ViewInject(R.id.area_et)
	EditText mAreaEt;
	@ViewInject(R.id.pro_visible_count_et)
	EditText mProVisibleCountEt;
	@ViewInject(R.id.pro_cyclic_ck)
	CheckBox mProCyclicCk;
	@ViewInject(R.id.city_cyclic_ck)
	CheckBox mCityCyclicCk;
	@ViewInject(R.id.area_cyclic_ck)
	CheckBox mAreaCyclicCk;
	@ViewInject(R.id.half_bg_ck)
	CheckBox mHalfBgCk;
	@ViewInject(R.id.reset_setting_tv)
	TextView mResetSettingTv;
	@ViewInject(R.id.submit_tv)
	TextView mSubmitTv;
	@ViewInject(R.id.result_tv)
	TextView mResultTv;
	@ViewInject(R.id.one_tv)
	TextView mOneTv;
	@ViewInject(R.id.two_tv)
	TextView mTwoTv;
	@ViewInject(R.id.three_tv)
	TextView mThreeTv;

	private int visibleItems = 5;

	private boolean isProvinceCyclic = true;

	private boolean isCityCyclic = true;

	private boolean isDistrictCyclic = true;

	private boolean isShowBg = true;

	private String defaultProvinceName = "江苏";

	private String defaultCityName = "常州";

	private String defaultDistrict = "新北区";

	public CityConfig.WheelType mWheelType = CityConfig.WheelType.PRO_CITY_DIS;

	@Override
	protected void initData(Bundle savedInstanceState) {

	}

	@Override
	protected String[] initPermissions() {
		return new String[0];
	}

	@Override
	protected int getLayoutId() {
		return R.layout.activity_city_picker_wheel;
	}

	@Override
	protected void initParams() {
		setTitleView("IOS城市选择器");
//提交
		mSubmitTv.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				wheel();
			}
		});

		//重置属性
		mResetSettingTv.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				reset();
			}
		});

		//一级联动，只显示省份，不显示市和区
		mOneTv.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mWheelType = CityConfig.WheelType.PRO;
				setWheelType(mWheelType);
			}
		});

		//二级联动，只显示省份， 市，不显示区
		mTwoTv.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mWheelType = CityConfig.WheelType.PRO_CITY;
				setWheelType(mWheelType);
			}
		});

		//三级联动，显示省份， 市和区
		mThreeTv.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mWheelType = CityConfig.WheelType.PRO_CITY_DIS;
				setWheelType(mWheelType);
			}
		});

		//省份是否循环显示
		mProCyclicCk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				isProvinceCyclic = isChecked;
			}
		});

		//市是否循环显示
		mCityCyclicCk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				isCityCyclic = isChecked;
			}
		});

		//区是否循环显示
		mAreaCyclicCk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				isDistrictCyclic = isChecked;
			}
		});

		//区是否循环显示
		mHalfBgCk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				isShowBg = isChecked;
			}
		});

		setWheelType(mWheelType);
	}

	/**
	 * 重置属性
	 */
	private void reset() {
		visibleItems = 5;
		isProvinceCyclic = true;
		isCityCyclic = true;
		isDistrictCyclic = true;
		isShowBg = true;

		defaultProvinceName = "江苏";
		defaultCityName = "常州";
		defaultDistrict = "新北区";

		mWheelType = CityConfig.WheelType.PRO_CITY_DIS;

		setWheelType(mWheelType);

		mProCyclicCk.setChecked(true);
		mCityCyclicCk.setChecked(true);
		mAreaCyclicCk.setChecked(true);

		mProEt.setText("" + defaultProvinceName);
		mCityEt.setText("" + defaultCityName);
		mAreaEt.setText("" + defaultDistrict);
		mProVisibleCountEt.setText("" + visibleItems);

		mHalfBgCk.setChecked(isShowBg);
		mProCyclicCk.setChecked(isProvinceCyclic);
		mAreaCyclicCk.setChecked(isDistrictCyclic);
		mCityCyclicCk.setChecked(isCityCyclic);

		setWheelType(mWheelType);
	}

	/**
	 * @param wheelType
	 */
	private void setWheelType(CityConfig.WheelType wheelType) {
		if (wheelType == CityConfig.WheelType.PRO) {
			mOneTv.setBackgroundResource(R.drawable.city_wheeltype_selected);
			mTwoTv.setBackgroundResource(R.drawable.city_wheeltype_normal);
			mThreeTv.setBackgroundResource(R.drawable.city_wheeltype_normal);
			mOneTv.setTextColor(Color.parseColor("#ffffff"));
			mTwoTv.setTextColor(Color.parseColor("#333333"));
			mThreeTv.setTextColor(Color.parseColor("#333333"));
		} else if (wheelType == CityConfig.WheelType.PRO_CITY) {
			mOneTv.setBackgroundResource(R.drawable.city_wheeltype_normal);
			mTwoTv.setBackgroundResource(R.drawable.city_wheeltype_selected);
			mThreeTv.setBackgroundResource(R.drawable.city_wheeltype_normal);
			mOneTv.setTextColor(Color.parseColor("#333333"));
			mTwoTv.setTextColor(Color.parseColor("#ffffff"));
			mThreeTv.setTextColor(Color.parseColor("#333333"));
		} else {
			mOneTv.setBackgroundResource(R.drawable.city_wheeltype_normal);
			mTwoTv.setBackgroundResource(R.drawable.city_wheeltype_normal);
			mThreeTv.setBackgroundResource(R.drawable.city_wheeltype_selected);
			mOneTv.setTextColor(Color.parseColor("#333333"));
			mTwoTv.setTextColor(Color.parseColor("#333333"));
			mThreeTv.setTextColor(Color.parseColor("#ffffff"));
		}
	}

	/**
	 * 弹出选择器
	 */
	private void wheel() {

		defaultProvinceName = mProEt.getText().toString();
		defaultCityName = mCityEt.getText().toString();
		defaultDistrict = mAreaEt.getText().toString();

		visibleItems = (Integer.parseInt(mProVisibleCountEt.getText().toString()));

		CityConfig cityConfig = new CityConfig.Builder(CityPickerWheelActivity.this).title("选择城市")
				.titleTextSize(18)
				.titleTextColor("#585858")
				.titleBackgroundColor("#E9E9E9")
				.confirTextColor("#585858")
				.confirmText("ok")
				.confirmTextSize(16)
				.cancelTextColor("#585858")
				.cancelText("cancel")
				.cancelTextSize(16)
				.setCityWheelType(mWheelType)
				.showBackground(isShowBg)
				.visibleItemsCount(visibleItems)
				.province(defaultProvinceName)
				.city(defaultCityName)
				.district(defaultDistrict)
				.provinceCyclic(isProvinceCyclic)
				.cityCyclic(isCityCyclic)
				.districtCyclic(isDistrictCyclic)
				.setCityWheelType(mWheelType)
				.setCustomItemLayout(R.layout.item_city)
				.setCustomItemTextViewId(R.id.item_city_name_tv)
				.build();

		CityPickerView.getInstance().setConfig(cityConfig);
		CityPickerView.getInstance().setOnCityItemClickListener(new OnCityItemClickListener() {
			@Override
			public void onSelected(ProvinceBean province, CityBean city, DistrictBean district) {
				StringBuilder sb = new StringBuilder();
				sb.append("选择的结果：\n");
				if (province != null) {
					sb.append(province.getName() + " " + province.getId() + " " + province.getGisBd09Lat() + " "
							+ province.getGisBd09Lng()).append("\n");
				}

				if (city != null) {
					sb.append(city.getName() + " " + city.getId() + " " + city.getGisBd09Lat() + " "
							+ city.getGisBd09Lng()).append("\n");
				}

				if (district != null) {
					sb.append(district.getName() + " " + district.getId() + " " + district.getGisBd09Lat() + " "
							+ district.getGisBd09Lng()).append("\n");
				}
				mResultTv.setText("" + sb.toString());
			}

			@Override
			public void onCancel() {
				JToastKit.getInstance().showMessage("已取消");
			}
		});
		CityPickerView.getInstance().showCityPicker(this);
	}
}
